# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : sub_process_option_view.py
# Time       ：2024/8/18 15:46
# Author     ：author name
# version    ：python 3.10
# Description：子流程option
"""
from flask_restx import fields

from apis.options import option_ns
from apis.options.schema.option_schema import NameSpaceOptionSchema
from apis.options.views import option_model
from apis.orderlines.models import Process
from public.api_utils.api_decorator import handle_api_error
from public.base_model import db
from public.base_response import generate_response, MarshalModel
from public.base_view import AbstractView

sub_process_opt = {'process_id': fields.String()}

sub_process_opt_marshal = MarshalModel('sub_process_opt', option_ns)


class SubProcessOptionView(AbstractView):
    url = '/sub_process_opt'

    def __init__(self, *args, **kwargs):
        super(SubProcessOptionView, self).__init__(*args, **kwargs)
        self.current_process_id = self.form_data.get('process_id')

    @handle_api_error()
    @option_ns.doc(description='获取子流程信息')
    @option_ns.expect(sub_process_opt_marshal.detail_request(sub_process_opt))
    @option_ns.marshal_with(sub_process_opt_marshal.list_response(option_model, as_list=True, is_db=False))
    def get(self):
        objs = db.session.query(Process).filter(
            Process.process_id != self.current_process_id, Process.active == 1
        ).all()

        sub_process_info = NameSpaceOptionSchema().dump(objs, many=True)
        return generate_response(sub_process_info)
